﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core;
using FluentValidation.Results;

namespace Microhard.WarehouseInsp.Buz.Validator
{
    public class ProductListModelValidator : AbstractValidator<ProductListModel>
    {
        public ProductListModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage(WarehouseMsg.NeedName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Name).Length(1, 20).WithMessage(WarehouseMsg.MaxNameLength).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                Custom(u =>
                {
                    IProductList iclt = IoC.Resolve<IProductList>();
                    if (iclt.IsNameDuplicate(u))
                    {
                        return new ValidationFailure("Name", WarehouseMsg.DuplicateName);
                    }
                    return null;
                });
            });
        }
    }
}
